package businesslogic.network;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import database.*;

public class RMIstubServer {
	
	BookPOService_Stub bookPOStub;
	MemberPOService_Stub memberPOStub;
	OrderItemPOService_Stub orderItemPOStub;
	OrderPOService_Stub orderPOStub;
	UserPOService_Stub userPOStub;
	CategoryPOService_Stub catagoryPOStub;
	PromotionPOService_Stub promotionPOStub;
	
	public RMIstubServer(){

		try {
			// init stub
			bookPOStub = new BookPOService_Stub();
			memberPOStub = new MemberPOService_Stub();
			orderPOStub = new OrderPOService_Stub();
			orderItemPOStub = new OrderItemPOService_Stub();
			userPOStub = new UserPOService_Stub();
			catagoryPOStub = new CategoryPOService_Stub();
			promotionPOStub = new PromotionPOService_Stub();
			
			// create ports
			LocateRegistry.createRegistry(3001);
			LocateRegistry.createRegistry(3008);
			LocateRegistry.createRegistry(3003);
			LocateRegistry.createRegistry(3004);
			LocateRegistry.createRegistry(3005);
			LocateRegistry.createRegistry(3006);
			LocateRegistry.createRegistry(3007);
			
			//bind ports
			Naming.bind("rmi://localhost:3001/BookPO", bookPOStub);
			Naming.bind("rmi://localhost:3008/CategoryPO", catagoryPOStub);
			Naming.bind("rmi://localhost:3003/MemberPO", memberPOStub);
			Naming.bind("rmi://localhost:3005/OrderPO", orderPOStub);
			Naming.bind("rmi://localhost:3004/OrderItemPO", orderItemPOStub);
			Naming.bind("rmi://localhost:3006/UserPO", userPOStub);
			Naming.bind("rmi://localhost:3007/PromotionPO", promotionPOStub);
			
			System.out.println("success");
			
		} catch (MalformedURLException | AlreadyBoundException | RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
